﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Windows.Forms;
using System.Xml;

public partial class FormTime : Form
{
    string strFile = "";

    public FormTime()
    {
        InitializeComponent();
    }

    private void monthCalendar1_DateSelected(object sender, DateRangeEventArgs e)
    {
        select_date(e);
    }

    private void select_date(DateRangeEventArgs e)
    {
        strFile = "";
        //this.richTextBox1.Text = e.Start.ToShortDateString();
        string s1 = e.Start.ToString("yyyy-MM-dd HH:mm:ss");
        string s = e.Start.ToString("yyyy-MM-dd");
        string nian = e.Start.ToString("yyyy");
        string yue = e.Start.ToString("MM");

        string strPath = get_path();

        string path = strPath + "/" + nian + "/" + yue;
        //if (!Directory.Exists(path))
        //{
        //    Directory.CreateDirectory(path);
        //}

        DirectoryInfo di = new DirectoryInfo(path);
        if (di.Exists)
        {
            //di.GetFiles
            foreach (FileInfo f in di.GetFiles())
            {
                if (f.Name.IndexOf(s) != -1)
                {
                    strFile = f.FullName;
                    break;
                }
            }
        }

        //strFile = Path.Combine(path, s + ".txt");

        if (File.Exists(strFile))
        {
            StreamReader sr = new StreamReader(strFile, Encoding.Default);
            string temp = sr.ReadToEnd();

            // if (temp.IndexOf("@大事") != -1) {
            //     //monthCalendar1.;
            //     //this.monthCalendar1.BackColor = Color.Blue;
            //     this.monthCalendar1.BoldedDates =
            //new System.DateTime[] { e.Start };
            // }

            this.myRichTextBox1.Text = temp;
            sr.Close();
        }
        else
        {
            strFile = Path.Combine(di.FullName, (s + " " + DateTime.Now.ToString("HH:mm:ss") + ".txt").Replace(":", ""));
            StringBuilder sb = new StringBuilder();
            sb.Append("心情=\n");
            sb.Append("温度=\n\n");
            this.myRichTextBox1.Text = sb.ToString();
        }

        this.myRichTextBox1.strFilename = strFile;
    }

    private static string get_path()
    {
        XmlDocument doc = new XmlDocument();
        doc.Load("xml/FormTime.xml");
        XmlNodeList x1 = doc.GetElementsByTagName("path");
        string strPath = "";
        foreach (XmlNode x in x1)
        {
            if (Directory.Exists(x.InnerText))
            {
                strPath = x.InnerText;
                break;
            }
        }
        return strPath;
    }

    /// <summary>
    ///
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void button1_Click(object sender, EventArgs e)
    {
        if (strFile != "")
        {
            myRichTextBox1.strFilename = strFile;
            myRichTextBox1.SaveDefault();
        }
    }

    private void FormTime_Load(object sender, EventArgs e)
    {
        monthCalendar1.SelectionStart = (DateTime.Now);
        //monthCalendar1.Select();
        monthCalendar1.ShowTodayCircle = true;
        //this.monthCalendar1.CalendarDimensions = new System.Drawing.Size(2, 1);

        //this.monthCalendar1.BoldedDates =
        //   new System.DateTime[] {new System.DateTime(2011, 6, 15, 0, 0, 0, 0),
        //                          new System.DateTime(2011, 6, 30, 0, 0, 0, 0)};

        //this.comboBox1.Items.Add("@大事");
        //this.comboBox1.Items.Add("@mm");
        //this.comboBox1.Items.Add("@请假");

        XmlDocument doc = new XmlDocument();

        doc.Load("Xml//keyword.xml");
        XmlNodeList nodeList = doc.SelectSingleNode("data").ChildNodes;
        XmlElement xe = null;

        foreach (XmlNode xn in nodeList)
        {
            xe = (XmlElement)xn;
            this.comboBox1.Items.Add(xe.InnerText.Trim());
        }

        this.comboBox1.SelectedIndex = 0;
    }

    private void monthCalendar1_DateChanged(object sender, DateRangeEventArgs e)
    {
        select_date(e);
    }

    private void panel_top_Paint(object sender, PaintEventArgs e)
    {
    }

    /// <summary>
    ///
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void button_chaxun_Click(object sender, EventArgs e)
    {
        string string_search = this.comboBox1.Text;
        this.monthCalendar1.BoldedDates = null;

        string nian = this.monthCalendar1.SelectionStart.ToString("yyyy");
        string yue = this.monthCalendar1.SelectionStart.ToString("MM");

        string path = get_path() + "/" + nian + "/" + yue;

        DirectoryInfo di = new DirectoryInfo(path);
        List<DateTime> l = new List<DateTime>();

        if (di.Exists)
        {
            //di.GetFiles
            foreach (FileInfo f in di.GetFiles())
            {
                StreamReader sr = new StreamReader(f.FullName, Encoding.Default);

                if (sr.ReadToEnd().IndexOf(string_search) != -1)
                {
                    string date = f.Name.Split(' ')[0];
                    DateTime d1 = Convert.ToDateTime(date);
                    l.Add(d1);
                }

                sr.Close();
            }

            this.monthCalendar1.BoldedDates = l.ToArray();
        }
    }

    private void panel_right_Paint(object sender, PaintEventArgs e)
    {
    }

    /// <summary>
    ///
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void button_jilu_Click(object sender, EventArgs e)
    {
        string nian = this.monthCalendar1.SelectionStart.ToString("yyyy");
        string yue = this.monthCalendar1.SelectionStart.ToString("MM");

        string path = get_path() + "/" + nian + "/" + yue;

        DirectoryInfo di = new DirectoryInfo(path);
        if (!di.Exists) { return; }

        List<DateTime> l = new List<DateTime>();

        foreach (FileInfo f in di.GetFiles())
        {
            StreamReader sr = new StreamReader(f.FullName, Encoding.Default);

            string date = f.Name.Split(' ')[0];
            DateTime d1 = Convert.ToDateTime(date);
            l.Add(d1);

            sr.Close();
        }

        this.monthCalendar1.BoldedDates = l.ToArray();
    }
}